<template>

    <div class="app-container" v-if="canList">
        <div class="filter-container">
            <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item input"
                      placeholder="用户编号"
                      v-model="listQuery.entity.topRecord.userCode">
            </el-input>
            <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item input"
                      placeholder="变动原因"
                      v-model="listQuery.entity.topRecord.info">
            </el-input>
            <el-date-picker
                    @change='handleFilter'
                    value-format="yyyy-MM-dd HH:mm:ss"
                    class="filter-item input"
                    v-model="listQuery.entity.topRecord.times"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="变动时间开始日期"
                    end-placeholder="变动时间结束日期">
            </el-date-picker>

            <div style="display: inline-block">

                <el-button style="margin-left: 10px;" class="filter-item" type="primary" v-waves icon="el-icon-search"
                           @click="handleFilter">查找
                </el-button>
<!--                <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary"-->
<!--                           v-if="canSave" icon="el-icon-edit">添加-->
<!--                </el-button>-->
                <el-button class="filter-item" type="primary" :loading="downloadLoading" v-waves icon="el-icon-download"
                           @click="handleDownload">导出
                </el-button>
<!--                <el-button class="filter-item" type="primary" :loading="downloadLoading" v-waves icon="el-icon-upload2"-->
<!--                           @click="()=>$refs.file.click()">导入-->
<!--                </el-button>-->

                <input style="display: none" ref="file" @change="upload" type="file">
            </div>
        </div>

        <el-table :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
                  style="width: 100%;">
            <yf-table-column align="center" label="ID">
                <template slot-scope="scope">
                    <span>{{ scope.row.topRecord.id }}</span>
                </template>
            </yf-table-column>
<!--            <yf-table-column align="center" label="用户ID">-->
<!--                <template slot-scope="scope">-->
<!--                    <span v-if="scope.row.topRecord">{{ scope.row.topRecord.userId }}</span>-->
<!--                </template>-->
<!--            </yf-table-column>-->
            <yf-table-column align="center" label="用户名">
                <template slot-scope="scope">
                    <span v-if="scope.row.topRecord">{{ scope.row.topRecord.userName }}</span>
                </template>
            </yf-table-column>
            <yf-table-column align="center" label="用户编号">
                <template slot-scope="scope">
                    <span v-if="scope.row.topRecord">{{ scope.row.topRecord.userCode }}</span>
                </template>
            </yf-table-column>
            <yf-table-column align="center" label="当前值">
                <template slot-scope="scope">
                    <span v-if="scope.row.topRecord">{{ scope.row.topRecord.topInfo }}</span>
                </template>
            </yf-table-column>
            <yf-table-column align="center" label="之前值">
                <template slot-scope="scope">
                    <span v-if="scope.row.topRecord">{{ scope.row.topRecord.oldInfo }}</span>
                </template>
            </yf-table-column>
<!--            <yf-table-column align="center" label="当前TID">-->
<!--                <template slot-scope="scope">-->
<!--                    <span v-if="scope.row.topRecord">{{ scope.row.topRecord.topId }}</span>-->
<!--                </template>-->
<!--            </yf-table-column>-->
<!--            <yf-table-column align="center" label="之前TID">-->
<!--                <template slot-scope="scope">-->
<!--                    <span v-if="scope.row.topRecord">{{ scope.row.topRecord.oldId }}</span>-->
<!--                </template>-->
<!--            </yf-table-column>-->
            <yf-table-column align="center" label="变动原因">
                <template slot-scope="scope">
                    <span v-if="scope.row.topRecord">{{ scope.row.topRecord.info }}</span>
                </template>
            </yf-table-column>
            <yf-table-column align="center" label="变动时间" width="160">
                <template slot-scope="scope">
                    <span v-if="scope.row.topRecord">{{ parseTime(scope.row.topRecord.time) }}</span>
                </template>
            </yf-table-column>

<!--            <yf-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">-->
<!--                <template slot-scope="scope">-->
<!--                    <el-button type="primary" v-if="canUpdate" size="mini" @click="handleUpdate(scope)">编辑</el-button>-->
<!--                    <el-button v-if="canSave" size="mini" type="danger"-->
<!--                               @click="handleDelete(scope)">删除-->
<!--                    </el-button>-->
<!--                </template>-->
<!--            </yf-table-column>-->

        </el-table>

        <div class="pagination-container" style="margin-top: 20px">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                           layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
            <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="120px"
                     style='width: 450px; margin-left:50px;'>
                <el-form-item label="用户ID" prop="topRecord.userId">
                    <el-input v-model="temp.topRecord.userId"></el-input>
                </el-form-item>
                <el-form-item label="用户名" prop="topRecord.userName">
                    <el-input v-model="temp.topRecord.userName"></el-input>
                </el-form-item>
                <el-form-item label="用户编号" prop="topRecord.userCode">
                    <el-input v-model="temp.topRecord.userCode"></el-input>
                </el-form-item>
                <el-form-item label="当前值" prop="topRecord.topInfo">
                    <el-input v-model="temp.topRecord.topInfo"></el-input>
                </el-form-item>
                <el-form-item label="之前值" prop="topRecord.oldInfo">
                    <el-input v-model="temp.topRecord.oldInfo"></el-input>
                </el-form-item>
                <el-form-item label="当前TID" prop="topRecord.topId">
                    <el-input v-model="temp.topRecord.topId"></el-input>
                </el-form-item>
                <el-form-item label="之前TID" prop="topRecord.oldId">
                    <el-input v-model="temp.topRecord.oldId"></el-input>
                </el-form-item>
                <el-form-item label="变动原因" prop="topRecord.info">
                    <el-input v-model="temp.topRecord.info"></el-input>
                </el-form-item>
                <el-form-item label="变动时间" prop="topRecord.time">
                    <el-date-picker v-model="temp.topRecord.time" type="datetime"
                                    value-format="yyyy-MM-dd HH:mm:ss" placeholder="变动时间">
                    </el-date-picker>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeDialogForm">取消</el-button>
                <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确认</el-button>
                <el-button v-else type="primary" @click="updateData">确认</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<style type="stylus">
.filter-container {
    /*padding-bottom: 20px;*/
}

.filter-item {
    margin-bottom: 10px;
}

.filter-item.input {
    /*line-height: 60px*/
    margin-right: 10px;
}
</style>

<script>
import {list, options, down, upload, info, save, update, remove} from '@/api/top_record'
import waves from '@/directive/waves' // 水波纹指令
import {downExcel} from '@/utils/down.js'
import {parseTime} from '@/utils'


import {mapGetters} from 'vuex'
import {hasAuth} from '@/utils/auth'

export default {
    name: 'test',
    directives: {
        waves
    },
    components: {},
    data() {
        return {
            tableKey: 0,
            list: null,
            total: null,
            listLoading: true,
            time: null
            ,
            listQuery: {
                page: 1,
                limit: 20,
                order: 'topRecord.id desc',
                entity: {
                    topRecord:
                        {
                            id: null,
                            userId: null,
                            userName: null,
                            userCode: null,
                            topInfo: null,
                            oldInfo: null,
                            topId: null,
                            oldId: null,
                            info: null,
                            time: null
                        }
                }
            },
            temp: {

                topRecord:
                    {
                        id: null,
                        userId: null,
                        userName: null,
                        userCode: null,
                        topInfo: null,
                        oldInfo: null,
                        topId: null,
                        oldId: null,
                        info: null,
                        time: null
                    }


            }
            ,
            dialogFormVisible: false,
            dialogStatus: '',
            textMap: {
                update: '修改',
                create: '新增'
            },
            rules:
                {
                    'topRecord.userId': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                    ,
                    'topRecord.userName': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                    ,
                    'topRecord.userCode': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                    ,
                    'topRecord.topInfo': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                    ,
                    'topRecord.oldInfo': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                    ,
                    'topRecord.topId': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                    ,
                    'topRecord.oldId': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                    ,
                    'topRecord.info': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                    ,
                    'topRecord.time': [
                        {
                            required: false,
                            message: '必填字段',
                            trigger: 'change'
                        }
                    ]
                }
            ,
            downloadLoading: false,
            qData:
                {
                    token: '',
                    key: ''
                }
        }
    },
    created() {
        this.getList()
        this.getOptions()
    },
    computed: {
        ...mapGetters([
            'perms'
        ]),
        canSave() {
            return hasAuth(this.perms, 'top:record:save')
        },
        canDelete() {
            return hasAuth(this.perms, 'top:record:delete')
        },
        canUpdate() {
            return hasAuth(this.perms, 'top:record:update')
        },
        canList() {
            return hasAuth(this.perms, 'top:record:list')
        }
    },
    methods: {
        parseTime(time) {
            if (!time) return ''
            return parseTime(time, '{y}-{m}-{d} {h}:{i}:{s}')
        },
        getList() {
            this.listLoading = true
            list(this.listQuery).then(response => {
                this.list = response.data.list
                this.total = response.data.total
                this.listLoading = false
            })
        },
        getOptions() {
            options().then(res => {
            })
        },
        handleFilter() {
            this.listQuery.page = 1
            this.getList()
        },
        handleSizeChange(val) {
            this.listQuery.limit = val
            this.getList()
        },
        handleCurrentChange(val) {
            this.listQuery.page = val
            this.getList()
        },
        handleModifyStatus(row, status) {
            this.$message({
                message: '操作成功',
                type: 'success'
            })
            row.status = status
        },
        beforeFormShow(row) {
            this.resetTemp(row) // 更新数据
        },
        afterFormShow(row) {

        },
        beforeFormCommit() {

        },
        resetTemp(row) {
            this.temp = {
                topRecord: {}
            }
            if (row) {
                this.temp = Object.assign({}, row) // copy obj
            }

        },
        handleCreate() {
            this.beforeFormShow()
            this.dialogStatus = 'create'
            this.dialogFormVisible = true
            this.$nextTick(() => {
                this.afterFormShow()
                this.$refs['dataForm'].clearValidate()
            })
        },
        handleDelete(scope) {
            this.index = scope.$index
            // this.beforeFormShow(scope.row)
            // this.temp = Object.assign({}, row) // copy obj
            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // this.$message({
                //   type: 'success',
                //   message: '删除成功!'
                // })
                this.deleteData()
            }).catch(() => {
                this.$message({
                    type: 'error',
                    message: '删除失败'
                })
            })
        },
        createData() {
            this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.beforeFormCommit()
                        save(this.temp).then(res => {
                            this.temp = res.data
                            // this.list.unshift(this.temp)
                            this.getList()
                            this.closeDialogForm()
                            this.$notify({
                                title: '成功',
                                message: '创建成功',
                                type: 'success',
                                duration: 2000
                            })
                        })
                    }
                }
            )
        },
        deleteData() {
            // const ids = [this.temp.topRecord.id]
            const ids = [this.list[this.index].topRecord.id]
            remove({ids: ids}).then(res => {
                // 从当前队列中移除
                this.list.splice(this.index, 1)
                this.$notify({
                    title: '成功',
                    message: '删除成功',
                    type: 'success',
                    duration: 2000
                })
            })
        },
        handleUpdate(scope) {
            this.beforeFormShow(scope.row)
            info(scope.row.topRecord.id).then(res => {
                this.temp = res.data
                this.dialogStatus = 'update'
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })
            })
        },
        updateData() {
            this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.beforeFormCommit()
                        update(this.temp).then(() => {
                            // this.list.splice(this.index, 1, this.temp)
                            this.getList()
                            this.closeDialogForm()
                            this.$notify({
                                title: '成功',
                                message: '更新成功',
                                type: 'success',
                                duration: 2000
                            })
                        })
                    }
                }
            )
        },
        closeDialogForm() {
            this.dialogFormVisible = false
        },
        upload(e) {
            upload(e.target.files[0]).then(res => {
                this.$notify({
                    title: '成功',
                    message: '上传成功!',
                    type: 'success',
                    duration: 2000
                })
                this.getList()
            })
        },
        handleDownload() {
            this.downloadLoading = true
            down(this.listQuery).then(res => {
                downExcel(res)
                this.downloadLoading = false
                this.$notify({
                    title: '成功',
                    message: '下载成功',
                    type: 'success',
                    duration: 2000
                })
            })
        },
        // formatJson(filterVal, jsonData) {
        //     return jsonData.map(v => filterVal.map(j => {
        //         return v[j]
        //     }
        // ))
        // },
    }
}
</script>
